<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import {selectCheckTotalByMonth} from "@/api/statistics/bigScreen";
const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      /** 获取施工状态实时统计 */
      selectCheckTotalByMonth({}).then(response => {
        this.initChart(response.data);
      });
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(charData) {
      this.chart = echarts.init(this.$el, 'macarons');

      let deptArray = [];
      let dataArray = [];

      for (let key in charData) {
        if (charData.hasOwnProperty(key)) { // 确保key是对象自身的属性
          let value = charData[key];
          deptArray.push(key);
          dataArray.push(value);
        }
      }

      this.chart.setOption(
        {
          title: {
            text: '当月检查统计',
            left: 'center'
          },
          tooltip: {},
          xAxis: {
            data: deptArray
          },
          yAxis: {},
          series: [
            {
              name: '当月检查数量',
              type: 'bar',
              data: dataArray
            }
          ]
        }
      )
    }
  }
}
</script>
